
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;

using Swaf;
using Swaf.Config;
using Swaf.Container;
using Swaf.DataAccess;

namespace Swaf.DataAccess
{
	public class DarExtensionsConfigPartner : IConfigPartner
	{
		protected IDictionary m_extensions = new ThreadSafeHashtable();

		public DarExtensionsConfigPartner(IApplication app)
		{
		}

		public IList getExtensionFeatures(string forFeature)
		{
			IList features = m_extensions[forFeature] as IList;
			if (features == null)
				m_extensions[forFeature] = features = new ArrayList();
			return features;
		}

		#region IConfigPartner Members
		public void registerForConfig(IConfigManager config)
		{
			config.registerHandler("... <DarExtensions>", new DarExtensionConfigHandler(this));
		}

		public void checkConfigStatus()
		{
			//Intentionally left blank. Nothing needs to be checked.
		}
		#endregion

		protected void registerExtension(string fromFeature, string forFeature)
		{
			IList features = m_extensions[forFeature] as IList;
			if (features == null)
				m_extensions[forFeature] = features = new ArrayList();
			features.Add(fromFeature);
		}

		protected class DarExtensionConfigHandler : ConfigHandler
		{
			private static string[] s_legalAttributes = new string[] { "from", "for" };
			private DarExtensionsConfigPartner m_mgr;

			public DarExtensionConfigHandler(DarExtensionsConfigPartner mgr)
			{
				m_mgr = mgr;
			}

			public override void executeBefore(IConfigElement configInfo)
			{
				configInfo.checkIllegalAttributes(s_legalAttributes);
				string fromFeature = configInfo.getAttribute("from");
				string forFeature = configInfo.getAttribute("for");
				m_mgr.registerExtension(fromFeature, forFeature);
			}
		}
	}
}
